//
//  QYGFetchingRecordVC.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/1.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGFetchingRecordVC.h"
#import "QYGMineFetchCell.h"
#import "QYGNetRequestModel.h"
#import "QYGGameRecordModel.h"


static NSString *fetchCell = @"fetchCell";
@interface QYGFetchingRecordVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *detailLabel;
@end

@implementation QYGFetchingRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cm_backgroudColor];
    self.title = @"抓取记录";
    [self setupUI];
    [self setupData];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.imgView.centerX = self.view.centerX;
    self.imgView.y = ScaleW(126);
    self.detailLabel.centerX = self.view.centerX;
    self.detailLabel.y = ScaleW(329);
}
- (void)dealloc{
    [NetRequestClass cancelOperations];
}
#pragma mark- tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.dataArray.count){
        
        self.imgView.hidden = YES;
        self.detailLabel.hidden = YES;
    }else{
        self.imgView.hidden = NO;
        self.detailLabel.hidden = NO;
    }
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return ScaleW(85);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QYGMineFetchCell *cell = [tableView dequeueReusableCellWithIdentifier:fetchCell forIndexPath:indexPath];
    QYGGameRecordModel *gameRecordModel = self.dataArray[indexPath.row];
    UIColor *color = gameRecordModel.operate_result == 1? [UIColor colorWithHex:@"29c956"] : [UIColor colorWithHex:@"A3A3A3"];
    NSString *accessoryStr = gameRecordModel.operate_result == 1? @"抓取成功" : @"抓取失败";
    [cell fetCellWithIconStr:gameRecordModel.ware_image nameStr:gameRecordModel.ware_name detailStr:gameRecordModel.created_at fetchCellMode:FetchCellModeRecord accessoryStr:accessoryStr accessColor:color isPick:NO];
    return cell;
}
#pragma mark - setData

- (void)setupData{
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark - setUI
- (void)setupUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.imgView];
    [self.view addSubview:self.detailLabel];
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
}
#pragma mark - loadData
- (void)loadDataWithPage:(NSInteger)page{
    
    NSInteger per_page = 20;
    [QYGNetRequestModel getMineGameRecordWithPage:page per_page:per_page operate_result:1 Completed:^(NSArray *mineGameArr) {
        [self endRefresh];
        [self.dataArray addObjectsFromArray:mineGameArr];
        [self.tableView reloadData];
        self.page = page;
    } WithErrorCodeBlock:^(NSError *error) {
        [self endRefresh];
    }];
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadMoreData{
    [self loadDataWithPage:self.page + 1];
}

- (void)loadNewData {
    self.dataArray = [NSMutableArray array];
    [self loadDataWithPage:1];
}
#pragma mark - lazy

- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        CGFloat navH = self.navigationController.navigationBar.height;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, navH + 20, 0);
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        [_tableView registerClass:[QYGMineFetchCell class] forCellReuseIdentifier:fetchCell];
    }
    return _tableView;
}
- (UIImageView *)imgView{
    if(!_imgView){
        _imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coin_line_bg"]];
        [_imgView sizeToFit];
    }
    return _imgView;
}

- (UILabel *)detailLabel{
    if(!_detailLabel){
        _detailLabel = [UILabel labelWithText:@"您目前还没有任何抓取记录" setFont:ScaleFont(15) setTextColor:[UIColor colorWithHex:@"999999"] setTextAlignment:NSTextAlignmentCenter];
        _detailLabel.numberOfLines = 1;
        [_detailLabel sizeToFit];
    }
    return _detailLabel;
}
@end
